package com.demiroot.server.pages;

import com.demiroot.server.HTTPResponse;
import com.demiroot.server.HTTPResquest;
import com.demiroot.server.HeaderData;
import com.demiroot.server.pages.JavaPageResponse;
import com.demiroot.server.pages.Page;

import MediaPlayer.MPlayer;

public class MediaControl implements Page {

	private static MPlayer player = new MPlayer();
	
	@Override
	public HTTPResponse getPage(HTTPResquest request) {
		HeaderData head = request.getHeader();
		if (head.getQuery().equals("play")) {
			if (player.currentlyPlaying() != null) {
				player.pause();
			}
		} else if (head.getQuery().equals("stop")) {
			player.stop();
		}  else if (head.getQuery().equals("fwd")) {
			player.fwd();
		}  else if (head.getQuery().equals("rwd")) {
			player.rwd();
		} else if (head.getQuery().startsWith("skip")) {
			double d = Double.parseDouble(head.getQuery().replaceAll("skip", ""));
			player.seek(d/400d);
		} else if (head.getQuery().equals("full")) {
			player.fullScreen();
		} else if (head.getQuery().startsWith("play=")) {
			player.playFile(head.getQuery().replace("play=", ""));
		} else if (head.getQuery().equals("full")) {
			player.fullScreen();
		} else if (head.getQuery().equals("mute")) {
			player.mute();
		}
		
		return new JavaPageResponse(getPageString());
	}

	public static boolean isThisYou(String file) {
		return file.equalsIgnoreCase("remote");
	}
	
	public String getPageString() {
		int size = 300;
		String s = " width=\"" + size + "\" height=\"" + size + "\"";
		return "<html><body>\n" +
				
				"<a href=\"remote?play\"><img src=\"play.jpg\" border=\"0\"" + s + "></a>" +
				"<a href=\"remote?stop\"><img src=\"stop.jpg\" border=\"0\"" + s + "></a><br>\n" +
				
				"<a href=\"remote?rwd\"><img src=\"rwd.jpg\" border=\"0\"" + s + "></a>" + 
				"<a href=\"remote?fwd\"><img src=\"fwd.jpg\" border=\"0\"" + s + "></a>" + 
				"<a href=\"folder\"><img src=\"folder.jpg\" border=\"0\"" + s + "></a>" + "<br>\n" +
								
				"<a href=\"remote?full\"><img src=\"fullscreen.jpg\" border=\"0\"" + s + "></a>" +
				"<a href=\"remote?mute\"><img src=\"mute.jpg\" border=\"0\"" + s + "></a><br>\n" +
				getProgressBar(450) +
				(player.isPlaying() ? progressJavaScript(450) : "") +
				"</body></html>";
	}
	
	private String progressJavaScript(int numBars) {
		
		int prog = (int)(player.getPercentDone()*100);
		int inc = (int) (player.getLength()*10*100/numBars);
		
		String ret = 	"<SCRIPT LANGUAGE=\"JavaScript\">\n" +
						"<!--\n" +
						
						"var curr=" + prog + ";\n" +
						"var inc=" + inc + ";\n" + 
						
						"function progress() {\n" +
						"	var target=document.getElementById(\"skip\" + curr);\n" +
						"	curr=curr+1;\n" + 
						"	target.src=\"full.jpg\";\n" +
						"	if (curr != 100) {\n" + 
						"		setTimeout(\"progress()\", inc);" +
						"	}\n" +
						"}\n" +
						
						"setTimeout(\"progress()\", inc);\n" +
						
						"//-->\n" +
						"</SCRIPT>";
		return ret;
	}

	private String getProgressBar(int numBars) {
		String s = "height=\"90\" width=\"2\"";
		String ret = "<br><img src=\"edge.jpg\" " + s + " border=\"0\">";
		int prog = (int)(player.getPercentDone()*numBars);
		for (int x = 0; x < prog;x++) {
			ret += "<a href=\"remote?skip" + x + "\"><img src=\"full.jpg\" " + s + " border=\"0\" id=\"skip" + x + "\"></a>";
		}
		for (int x = prog; x < numBars;x++) {
			ret += "<a href=\"remote?skip" + x + "\"><img src=\"open.jpg\" " + s + " border=\"0\" id=\"skip" + x + "\"></a>";
		}
		ret += "<img src=\"edge.jpg\" " + s + " border=\"0\">";
		return ret;
	}
}
